package com.lagou.config;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

public class XMLMapperBuilder {
    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    public void parse(InputStream inputStream) throws DocumentException,
            ClassNotFoundException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();

        String namespace = rootElement.attributeValue("namespace");
        List<Element> select = rootElement.selectNodes("select");
        List<Element> insert = rootElement.selectNodes("insert");
        List<Element> update = rootElement.selectNodes("update");
        List<Element> delete = rootElement.selectNodes("delete");

        if (null != select) {
            for (Element element : select) { //id的值
                String id = element.attributeValue("id");
                String paramterType = element.attributeValue("paramterType");
                String resultType = element.attributeValue("resultType"); //输入参数class

                //sql语句
                String textTrim = element.getTextTrim();
                //封装 mappedStatement
                MappedStatement mappedStatement = new MappedStatement();
                mappedStatement.setId(id);
                mappedStatement.setParamterType(paramterType);
                mappedStatement.setResultType(resultType);
                mappedStatement.setSql(textTrim);
                //statementId
                String key = namespace + "." + id;
                //填充 configuration
                configuration.getMappedStatementMap().put(key, mappedStatement);
            }
        }
        if (null != insert) {
            for (Element element : insert) {
                String id = element.attributeValue("id");
                String paramterType = element.attributeValue("paramterType");
                String resultType = element.attributeValue("java.lang.Boolean");
                //sql语句
                String textTrim = element.getTextTrim();
                //封装 mappedStatement
                MappedStatement mappedStatement = new MappedStatement();
                mappedStatement.setId(id);
                mappedStatement.setParamterType(paramterType);
                mappedStatement.setResultType(resultType);
                mappedStatement.setSql(textTrim);
                //statementId
                String key = namespace + "." + id;
                //填充 configuration
                configuration.getMappedStatementMap().put(key, mappedStatement);
            }
        }
        if (null != update) {
            for (Element element : update) {
                String id = element.attributeValue("id");
                String paramterType = element.attributeValue("paramterType");
                //sql语句
                String textTrim = element.getTextTrim();
                //封装 mappedStatement
                MappedStatement mappedStatement = new MappedStatement();
                mappedStatement.setId(id);
                mappedStatement.setParamterType(paramterType);
                mappedStatement.setSql(textTrim);
                //statementId
                String key = namespace + "." + id;
                //填充 configuration
                configuration.getMappedStatementMap().put(key, mappedStatement);
            }
        }
        if (null != delete){
            for (Element element : delete) {
                String id = element.attributeValue("id");
                String paramterType = element.attributeValue("paramterType");
                //sql语句
                String textTrim = element.getTextTrim();
                //封装 mappedStatement
                MappedStatement mappedStatement = new MappedStatement();
                mappedStatement.setId(id);
                mappedStatement.setParamterType(paramterType);
                mappedStatement.setSql(textTrim);
                //statementId
                String key = namespace + "." + id;
                //填充 configuration
                configuration.getMappedStatementMap().put(key, mappedStatement);
            }
        }


    }
}